﻿using System;
using System.Configuration;
using Logger;
using System.Data;
using System.Reflection;
using ClickLand.CorporacionPlaza.AccesoDato.Contratos;
using ClickLand.CorporacionPlaza.Comunes.Entidades;
using ClickLand.CorporacionPlaza.Comunes.Recursos;
using Logger;
using MySql.Data.MySqlClient;

namespace ClickLand.CorporacionPlaza.AccesoDato.Implementacion.NHibernateMySql
{
    /// <summary> Clase que implementa el contrato IDaoPension  </summary>
    public class DaoPensionNHibernateMySql : DaoBase<Pension>, IDaoPension
    {
        public Pension ConsultarPorContrato(object idContrato)
        {
            #region Instrumentacion Entrada
            if (Log.InfoHabilitado)
                Logger.Log.Info(string.Format(Recurso.EntrandoMetodo,MethodBase.GetCurrentMethod().Name));
            #endregion
            Pension retorno = new Pension();

            try
            {

                MySqlCommand comando = new MySqlCommand();
                comando.Connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConexionMysql"].ToString());
                comando.CommandType = CommandType.StoredProcedure;
                comando.CommandText = "ConsultarPensionContrato";
                comando.Connection.Open();
                comando.Parameters.AddWithValue("@IdContrato", idContrato);

                comando.Parameters["@IdContrato"].Direction = ParameterDirection.Input;
                MySqlDataReader reader = comando.ExecuteReader();
                while (reader.Read())
                {
                    retorno = new Pension();
                    retorno.Id = (long)reader[0];
                    retorno.Periodo = (int)reader[1];
                    retorno.Tipo = reader[2].ToString();
                    retorno.Estado = (int)reader[3];
                }

                comando.Connection.Close();
                return retorno;
            }
            catch (Exception e)
            {
                #region Instrumentacion Error
                if (Log.ErrorHabilitado)
                    Logger.Log.Info(e.Message + " " + e.InnerException);
                #endregion
            }
            finally
            {
                Session.Close();
            }
            #region Instrumentacion Salida
            if (Log.InfoHabilitado)
                Logger.Log.Info(string.Format(Recurso.SaliendoMetodo,MethodBase.GetCurrentMethod().Name));
            #endregion
            return retorno;
        }
    }
}
